﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Eis51.CampanyCMS.Model.NewsManager;
using Eis51.CampanyCMS.Business.NewsManager;
using Eis51.CampanyCMS.Business;
using System.IO;
using Eis51.CampanyCMS.Business.OnlineService;

namespace Eis51.CampanyCMS.UI.Admin.test
{
    public partial class Isdata : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
 

        protected void bntCreate_Click(object sender, EventArgs e)
        {
           
            string sql = "select * from NewsType where parentid=0";
            DataTable dt = BaseChatToolsBLL.GetInstance().GetDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sql = string.Format("select * from  NewsType where parentid={0}", dt.Rows[i]["Seqno"].ToString());
                DataTable temp = BaseChatToolsBLL.GetInstance().GetDataTable(sql);
                if (temp != null && temp.Rows.Count > 0)
                {
                    for (int j = 0; j < temp.Rows.Count; j++)
                    {
                        InserNews(temp.Rows[j], i, j);
                    }
                }
                else
                {
                    InserNews(dt.Rows[i], i, 0);
                }
            }
        }

        private void InserNews(DataRow dr ,int i,int j)
        {

            for (int n = 0; n < 4; n++)
            {
                News news = new News();
                news.Licd =int.Parse ( dlist.SelectedValue);
                news.Filename = "";
                news.Title = "测试数据" + dr["Title"].ToString() + i.ToString() + "_" + j.ToString() + "_" + n.ToString();
                news.Code = "";
                news.Showdescr = "";
                news.Type = int.Parse(dr["Seqno"].ToString());
                news.Content = "测试数据" + dr["Title"].ToString() + i.ToString() + "_" + j.ToString() + "_" + n.ToString();
                news.IsValid = 1;
                
                news.Createby = txtUserName.Text ;
                news.Lasteupdateby = txtUserName.Text;

                NewsBLL.GetInstance().Save(news);
            }
        }

        protected void bntSave_Click(object sender, EventArgs e)
        {
            string fpath= UploadFile.PostedFile.FileName ;
            StreamReader sr = new StreamReader(fpath);
            while (sr.Read() != -1)
            {
                string text = sr.ReadLine();
                InserNewType(text); 
            }
            sr.Close();
        }

        private void InserNewType(string text)
        {
            string Main = "";
            string Sub="";
            if (text.IndexOf('[')!=-1)
            {
                Main = text.Substring(0, text.IndexOf("["));
                Sub = text.Substring(text.IndexOf("[") + 1,text.IndexOf("]")- text.IndexOf("[")-1);
            }
            else
            {
                Main = text ;
            }
            string[] MainArr = Main.Split('|');
            if (MainArr != null && MainArr.Length == 2)
            {
               int parentid=  SaveNewType(MainArr[0], MainArr[1], 0);
                if (Sub != string.Empty)
                {
                    if (Sub.IndexOf("@")==-1)
                    {
                        Sub += "@";
                    }
                    string[] subArr = Sub.Split('@');
                    foreach (string a in subArr)
                    {
                        if (a != string.Empty)
                        {
                            string[] subArr1 = a.Split('|');
                            int t = SaveNewType(subArr1[0], subArr1[1], parentid);
                        }
                    }
                }
            }

        
        }

        private int  SaveNewType(string Newstypename, string Code ,int parentid)
        {
            Newstype n = new Newstype();
            n.Parentid = 0;
            n.Createby = txtUserName.Text;
            n.Lasteupdateby = txtUserName.Text;
            n.Licd = int.Parse(dlist.SelectedValue);
            n.Title  = Newstypename;
            n.Code = Code;

            NewstypeBLL.GetInstance().Save(n);
            return n.Seqno;
        }

       
    }
}
